package com.mlethe.library.qrcode.zxing;

import android.os.Handler;
import android.os.Looper;

import com.google.zxing.DecodeHintType;

import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
 * 解码线程
 */
final class DecodeThread extends Thread {

    private final CountDownLatch mHandlerInitLatch;
    private DecodeHandler mHandler;

    private Handler mCallbackHandler;

    DecodeThread(Handler handler) {
        mCallbackHandler = handler;
        mHandlerInitLatch = new CountDownLatch(1);
    }

    Handler getHandler() {
        try {
            mHandlerInitLatch.await();
        } catch (InterruptedException ie) {
            // continue?
        }
        return mHandler;
    }

    /**
     * 设置识别的格式
     *
     * @param barcodeType 识别的格式
     * @param hintMap     barcodeType 为 BarcodeType.CUSTOM 时，必须指定该值
     */
    public void setType(BarcodeType barcodeType, Map<DecodeHintType, Object> hintMap) {
        if (mHandler != null) {
            mHandler.setType(barcodeType, hintMap);
        }
    }

    @Override
    public void run() {
        Looper.prepare();
        mHandler = new DecodeHandler();
        mHandlerInitLatch.countDown();
        Looper.loop();
    }

}
